# Makefile for prime sieve benchmarks
N ?= 10000000
RUNS ?= 5
CC = clang
CFLAGS = -O3 -lm
RUSTC = rustc
JAVAC = javac
JAVA = java
PYTHON = python3
NODE = node

.PHONY: all clean run run_c run_rust run_java run_python run_js

all: c_sieve rust_sieve Sieve.class

# C build
c_sieve: bench.c
	$(CC) $(CFLAGS) -o $@ $<

run_c: c_sieve
	@echo "Running C benchmark (N=$(N), runs=$(RUNS)..."
	@./c_sieve $(N) $(RUNS) | tee -a results.txt

# Rust build
rust_sieve: bench.rs
	$(RUSTC) -O -o $@ $<

run_rust: rust_sieve
	@echo "Running Rust benchmark (N=$(N), runs=$(RUNS)..."
	@./rust_sieve $(N) $(RUNS) | tee -a results.txt

# Java build
Sieve.class: Sieve.java
	$(JAVAC) $<

run_java: Sieve.class
	@echo "Running Java benchmark (N=$(N), runs=$(RUNS)..."
	@$(JAVA) Sieve $(N) $(RUNS) | tee -a results.txt

# Python run
run_python:
	@echo "Running Python benchmark (N=$(N), runs=$(RUNS)..."
	@$(PYTHON) bench.py $(N) $(RUNS) | tee -a results.txt

# JavaScript run
run_js:
	@echo "Running JavaScript benchmark (N=$(N), runs=$(RUNS)..."
	@$(NODE) sieve.js $(N) $(RUNS) | tee -a results.txt

# Run all benchmarks
run: clean all run_rust run_c run_java run_js run_python
	@echo "\n=== Final Results ==="
	@cat results.txt

# Clean build artifacts
clean:
	rm -f c_sieve rust_sieve *.class results.txt